using OhioState.AssetManaged;
using OhioState.Graphics;
using OhioState.Graphics.Proxy;
using Tao.OpenGl;

namespace OhioState.Graphics.OpenGL
{
    public class TexImageHandler : TextureHandler
    {
               
        public TexImageHandler(IAssetManaged proxyObject)
            : base(proxyObject)
        {
        }
    
        
        protected override void createGLTexture()
        {
            _realTexture = new Texture2DGL(_textureProxy.Width, _textureProxy.Height, _textureProxy.TexelType);
        }
        protected override void updateGLTexture()
        {
            TexImage texImage = _textureProxy as TexImage;
            if (texImage != null)
            {
                int xDim, yDim;
                byte[] byte_buffer;
                uint[] uint_buffer;
                ushort[] ushort_buffer;
                float[] float_buffer;
                int format = TextureManager.PixelFormat[texImage.TexelType.Format];
                int type = TextureManager.PixelBuffer[texImage.TexelType.Type];
                
                switch (texImage.TexelType.Type)
                {
                    case ByteFormat.BYTE:
                        //byte_buffer = texImage.GetTextureByte(out xDim, out yDim);
                        //byte[, ,] texelsByte;
                        //texImage.GetTexture(out texelsByte);
                        //Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0,
                        //    texelsByte.GetLength(0), texelsByte.GetLength(1),
                        //    format, type, texelsByte);
                        // TODO: Fix Oleg's Preintegrated transfer function so we can use the above.
                        byte[] texelsByte;
                        texImage.GetTexture(out texelsByte);
                        Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0,
                            this.Width, this.Height,
                            format, type, texelsByte);
                        break;
                    case ByteFormat.INT:
                        uint[,,] texelsInteger;
                        texImage.GetTexture(out texelsInteger);
                        Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0,
                            texelsInteger.GetLength(0), texelsInteger.GetLength(1),
                            format, type, texelsInteger);
                        break;
                    case ByteFormat.SHORT:
                        ushort_buffer = texImage.GetTextureShort(out xDim, out yDim);
                        Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0,
                            xDim, yDim,
                            format, Gl.GL_UNSIGNED_BYTE, ushort_buffer);
                        break;
                    case ByteFormat.UNSIGNED_SHORT://added by me (Oleg) for preintegrated TF support
                        ushort_buffer = texImage.GetTextureShort(out xDim, out yDim);
                        Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0,
                            xDim, yDim,
                            Gl.GL_RGBA, Gl.GL_UNSIGNED_SHORT, ushort_buffer);
                        break;
                    case ByteFormat.FLOAT:
                        float_buffer = texImage.GetTextureFloat(out xDim, out yDim);
                        Gl.glTexSubImage2D(Gl.GL_TEXTURE_2D, 0, 0, 0,
                            xDim, yDim,
                            format, type, float_buffer);
                        break;
                }
                //Gl.glDisable(Gl.GL_TEXTURE_3D);
                
            }
        }
    }
}
